package qc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.quickconnect.ControlObject;

import ui.MainFrame;

import com.mysql.jdbc.Connection;

import beans.MessageBean;

public class ParsePrivateMessageBCO implements ControlObject{

	@Override
	public Object handleIt(ArrayList<Object> arg0) {
		HashMap params = (HashMap) arg0.get(arg0.size()-1);
		MessageBean msg = (MessageBean) params.get("message");
		String to = (String) params.get("userTo");
		
		Connection con = (Connection) MainFrame.mainFrame.getController().getConnectionPool().getConnection();
		java.sql.PreparedStatement insert = null;
		boolean success = false;
		try {
			insert = con.prepareStatement("INSERT INTO PrivateMessage (FromID, ToID, SessionID, MessageSendTime, Message) VALUES ((SELECT UserID FROM User WHERE UserName = ?),(SELECT UserID FROM User WHERE UserName = ?),?,?)");
			insert.setString(1, msg.getSender());
			insert.setString(2, to);
			insert.setTimestamp(3, msg.getTimestamp());
			insert.setString(4, msg.getMessage());
			insert.execute();
			success = true;
			
		} catch (SQLException e) {
			success = false;
		}
		params.put("success", success);
		return params;
	}

}
